# Maintainer: d0p1 <contact@d0p1.eu>

_realname=liboqs
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.13.0
pkgrel=1
pkgdesc="C library for prototyping and experimenting with quantum-resistant cryptography (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://openquantumsafe.org/'
msys2_repository_url="https://github.com/open-quantum-safe/liboqs"
license=('spdx:MIT')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-xdist"
  "${MINGW_PACKAGE_PREFIX}-python-yaml"
)
source=("https://github.com/open-quantum-safe/liboqs/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-aarch64-detection.patch")
sha256sums=('789e9b56bcb6b582467ccaf5cdb5ab85236b0c1007d30c606798fa8905152887'
            '99a7133701b106e6e0d4d9dece250a2e63c1082c7c6835747ad69387bd29f958')

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    001-aarch64-detection.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CC} == clang ]]; then
    export CFLAGS+=" -Wno-unknown-warning-option"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DBUILD_SHARED_LIBS=ON \
      -DOQS_BUILD_ONLY_LIB=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
